/*
* Copyright (c) 2007, Nubo Technologies
* 
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the Nubo Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.nubotech.gwt.oss.client.s3;

import com.google.gwt.http.client.Header;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.Response;
import com.nubotech.gwt.oss.client.Bucket;
import com.nubotech.gwt.oss.client.ObjectHolder;
import com.nubotech.gwt.oss.client.ObjectHolderResult;
import com.nubotech.gwt.oss.client.ObjectHolderResultCallback;
import com.nubotech.gwt.oss.client.OnlineStorageServiceError;
import com.nubotech.gwt.oss.client.util.DateUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author jonnakkerud
 */
public class ObjectHolderHandler extends AwsS3CallbackHandler {
    public static final String AMZ_META_HEADER = "x-amz-meta-";
    
    private ObjectHolderResultCallback objectCallback;
    private String bucketName;
    private String key;
    private boolean handleData = false;
    
    public ObjectHolderHandler(String bucketName, String key, ObjectHolderResultCallback objectCallback, boolean handleData) {
        this.objectCallback = objectCallback;
        this.handleData = handleData;
        this.bucketName = bucketName;
        this.key = key;
    }

    public void onResponseReceived(Request request, Response response) {
        ObjectHolderResult objectHolderResult;
        if (hasError(response)) {
            objectHolderResult = createResult(createError(response));
        } else {
            ObjectHolder c = new ObjectHolder();
            c.setBucket(new Bucket(bucketName));
            c.setKey(this.key);
            
            Header[] headers = response.getHeaders();
            
            // extract meta data
            for (int i = 0; i < headers.length; i++) {
                Header h = headers[i];
                String name = h.getName();
                if (name.equalsIgnoreCase("Content-Type")) {
                    c.setContentType(h.getValue());
                } else if (name.equalsIgnoreCase("Content-Length")) {
                    c.setSize(Integer.parseInt(h.getValue()));
                } else if (name.equalsIgnoreCase("Last-Modified")) {                    
                    Date d = DateUtils.parseHTTPDate(h.getValue());
                    c.setLastModified(d);
                } else if (name.equalsIgnoreCase("ETag")) {
                    c.setETag(h.getValue());
                    
                } else if (name.startsWith(AMZ_META_HEADER)) {
                    String metaName = name.substring(AMZ_META_HEADER.length());
                    c.addMetaData(metaName, h.getValue());
                }
            }
            
            // get the data
            if (handleData) {
                c.setData(response.getText());
            }
            
            List l = new ArrayList();
            l.add(c);
            objectHolderResult = new ObjectHolderResult(l, null);
        }
        
        objectCallback.onObjectHolderResult(objectHolderResult);
    }
    
    public void onError(Request request, Throwable ex) {
        objectCallback.onObjectHolderResult(new ObjectHolderResult(new ArrayList(), new OnlineStorageServiceError(ex)));        
    }
    
    private static ObjectHolderResult createResult(OnlineStorageServiceError error) {
        return new ObjectHolderResult(new ArrayList(), error);
    }
    
}
